package cn.egame.extraction.multithread.service.filter2;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import cn.egame.common.efs.IFileSystem;
import cn.egame.common.efs.SFileSystemClient;
import cn.egame.common.exception.ExceptionCommonBase;
import cn.egame.common.web.ExceptionWeb;
import cn.egame.extraction.multithread.pojo.EfsInfo;
import cn.egame.extraction.multithread.service.CrawlerThread;
import cn.egame.extraction.multithread.util.ConstVar_EFS;
import cn.egame.extraction.multithread.util.EnumType_EFS.InputErrorFileAppendType;
import cn.egame.extraction.multithread.util.Util;

public class CrawlerFileFilter extends Filter {
	
	
	/**
	 * @throws ExceptionWeb 
	 * @Description: 抓取文件
	 * @Author yuchao
	 * @Create Date  2013-9-30
	 * @Modified by none
	 * @Modified Date
	 */
	private int catchFile(int catchTimes, EfsInfo efsInfo, String filePath, CrawlerThread currentThread) throws Exception{
		if(catchTimes>ConstVar_EFS.REPEAT_READ_NUM)
			return catchTimes;
		InputStream inputStream = null;
		String writePath = ConstVar_EFS.UPLOAD_WRITE_ADDRESS + filePath;
		File file = null;
		try {
			currentThread.logger.info("开始第"+(catchTimes)+"次拉取id为"+efsInfo.getEfs_id()
					+"类型为"+efsInfo.getEfs_type()+",路径为"+ConstVar_EFS.DNS_HOST+filePath+"的文件");
			
			inputStream = Util.getInputStreamFromUrl(ConstVar_EFS.DNS_HOST + filePath);
			long size = Util.getInputStreamLengthFromUrl(ConstVar_EFS.DNS_HOST + filePath);
			IFileSystem fileSystem = SFileSystemClient.getInstance("mobile");
			currentThread.logger.info("writePath:" + writePath);
            file = new File(writePath);
            fileSystem.mkdirs(writePath);
            int fileSize = fileSystem.uploadFile(writePath, inputStream);
            if(fileSize!=size){
            	throw new Exception("文件拉取失败");
            }
//            currentThread.successSyncIdLog.info("id为"+efsInfo.getEfs_id()+"类型为:"+efsInfo.getEfs_type()+"的文件拉取成功, 对应的url为:"+ConstVar_EFS.DNS_HOST+filePath);
		} catch(ExceptionWeb ew){
			if(file!=null){
				file.delete();
			}
			throw ew;
		} catch (Exception e) {
			if(file!=null){
				file.delete();
			}
			return catchFile(++catchTimes, efsInfo, filePath, currentThread);
		} finally{
			if(inputStream!=null){
				try {
					inputStream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return catchTimes;
	}

	@Override
	public void doFilter(ChainObject chainObject, FilterChain filterChain)
			throws ExceptionCommonBase {
		EfsInfo efsInfo = (EfsInfo) chainObject.get("efsInfo");
		CrawlerThread currentThread = (CrawlerThread) chainObject.get("currentThread");
		List<String> filePathList = (List<String>) chainObject.get("filePathList");
		if(efsInfo!=null
				&& currentThread!=null && filePathList!=null){
			for (String fp : filePathList) {
				try {
					int i = catchFile(1, efsInfo, fp, currentThread);
//					localfilePath = fp;
					File file = new File(ConstVar_EFS.UPLOAD_ADDRESS+fp);//TODO
					long length = Util.getInputStreamLengthFromUrl(ConstVar_EFS.DNS_HOST + fp);
					if (i > ConstVar_EFS.REPEAT_READ_NUM || !file.exists()
							|| file.length()!=length) {
						currentThread.logger.error("(i > ConsVar_EFS.REPEAT_READ_NUM) --->"+(i > ConstVar_EFS.REPEAT_READ_NUM));
						currentThread.logger.error("(!file.exists()) --->"+(!file.exists()));
						currentThread.logger.error("(file.length()!=length) --->"+(file.length()!=length));
						throw new ExceptionCommonBase(-1, "文件拉取失败");	
					}
				} catch(ExceptionWeb ew){
					currentThread.logger.error(ew);
					currentThread.errorSyncIdLog.error("id为"+efsInfo.getEfs_id()+"类型为:"+efsInfo.getEfs_type()+"的文件拉取失败,异常信息为:"+ew.getMessage()+" 对应的url为:"+ConstVar_EFS.DNS_HOST+fp);
					currentThread.getFileService().fetchOkAndWriteBack(
							currentThread.getFilename(), efsInfo);
					currentThread.getFileService().writeBackToFilePath(ConstVar_EFS.ERROR_IDS_FILE_PATH_404,
							InputErrorFileAppendType.append, efsInfo, 1);
				} catch (Exception e) {
					currentThread.logger.error(e);
					currentThread.errorSyncIdLog.error("id为"+efsInfo.getEfs_id()+"类型为:"+efsInfo.getEfs_type()+"的文件拉取失败, 对应的url为:"+ConstVar_EFS.DNS_HOST+fp);
					currentThread.getFileService().fetchOkAndWriteBack(currentThread.getFilename(), efsInfo);
					currentThread.getFileService().writeBackToFilePath(ConstVar_EFS.ERROR_IDS_FILE_PATH_500,
							InputErrorFileAppendType.append, efsInfo, 1);
				}
			}
			chainObject.put("checkNum", 2);
			filterChain.doFilter(chainObject);
		}
	}

}
